#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use Bio::DB::Fasta;
use Bio::SeqIO;

# Run hmmsearch or hmmscan with the
# --tblout FILENAME option to save the output in a tab delimited file
#
# to run this script you type
# DIRECTORYOFFASTASEQUENCES is either directory or single file of fasta sequences
# make sure  the files in the directory end with .fa or .fasta or .fas

# HMMEROUT.tblout is the output from HMMER3 with the --tblout option
# perl hmmer3_filter.pl --db DIRECTORYOFFASTASEQUENCES HMMEROUT.tblout

## 
my $cutoff = 1e-10;
my $db;
GetOptions(
	   'c|cutoff:s' => \$cutoff,
	   'db:s'       => \$db,	   
	   );

my %unique;
while(<>) {
 next if /^\#/;
 chomp;
 my ($target,$targetacc,$query,$queryacc,$evalue,$score) = split(/\s+/,$_);
 next unless $evalue <= $cutoff;
 $unique{$target}++;
} 

if( $db ) {
 my $dbh = Bio::DB::Fasta->new($db,-glob => '*.{fa,fas,fasta,pep,peps,aa,nt,seq}');
 my $out = Bio::SeqIO->new(-format => 'fasta');
 for my $t ( keys %unique) {
   $out->write_seq($dbh->get_Seq_by_acc($t));
 }
} else {
 for my $t ( keys %unique) {
  print $t, "\n";
 }
}
